package com.zhengdongzulin.zuke.app.main.mine.bill

import android.text.TextUtils
import androidx.fragment.app.Fragment
import com.google.gson.Gson
import com.lxj.androidktx.core.getVM
import com.lxj.androidktx.core.sp
import com.lxj.androidktx.core.toast
import com.zhengdongzulin.zuke.R
import com.zhengdongzulin.zuke.app.data.ChangeHomeData
import com.zhengdongzulin.zuke.base.BaseActivity
import com.zhengdongzulin.zuke.databinding.ZkActivityBillBinding
import com.zhengdongzulin.zuke.ext.getCenterDialog

/**
 * 账单
 */
class ZKBillActivity : BaseActivity<BillVm, ZkActivityBillBinding>() {

    var mTitles = arrayOf("待缴账单", "历史账单")
    var mFragments = arrayListOf<Fragment>()

    //    var mHouseId :String = ""
    lateinit var mChangeHomeData: ChangeHomeData

    override fun getLayoutResId(): Int = R.layout.zk_activity_bill

    override fun initVM(): BillVm = getVM(BillVm::class.java)

    override fun initView() {
        setTitleContent("我的账单")
        mFragments.add(ZKBillFragment.newInstance(ZKBillFragment.BillType.UNPAID_BILL))
        mFragments.add(ZKBillFragment.newInstance(ZKBillFragment.BillType.HISTORY_BILL))
    }

    override fun initData() {
        if (!TextUtils.isEmpty(sp().getString("change_home", ""))) {
            mChangeHomeData =
                Gson().fromJson(sp().getString("change_home", ""), ChangeHomeData::class.java)
            mView.tvHouse.text = mChangeHomeData.houseName + mChangeHomeData.houseNumber
            mView.tvHouseAddress.text =
                mChangeHomeData.startDate.replace("-", ".") + "-" + mChangeHomeData.endDate.replace(
                    "-",
                    "."
                )
            mView.tlTitle.setViewPager(mView.vpFragment, mTitles, this, mFragments)
            mView.tlTitle.onPageSelected(0)
        }
    }

    override fun initListener() {
        mShareViewModel.changeHomeData.value?.let {
            setRightButton("切换房间", click = {
                toast("切换房间啦")
                getCenterDialog(mShareViewModel.changeHomeData.value!!, disimiss = {
                    mChangeHomeData = it

                    mView.tvHouse.text = mChangeHomeData.houseName + mChangeHomeData.houseNumber
                    mView.tvHouseAddress.text = mChangeHomeData.startDate.replace(
                        "-",
                        "."
                    ) + "-" + mChangeHomeData.endDate.replace("-", ".")

                    notifyBill()
                }).show()
            })
        }
    }

    /**
     * 更新账单
     */
    private fun notifyBill() {
        for (fragment in mFragments) {
            (fragment as ZKBillFragment).initData()
        }
    }

    override fun startObserve() {

    }

}